package com.cug.campus_sh_market_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cug.campus_sh_market_server.entity.myenum.GoodsStatusEnum;
import com.cug.campus_sh_market_server.entity.myenum.UserTypeEnum;
import com.cug.campus_sh_market_server.entity.param.OrderParam;
import com.cug.campus_sh_market_server.entity.param.PageParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsAddParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsPutParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsQueryParam;
import com.cug.campus_sh_market_server.entity.param.goods.GoodsStatusUpdateParam;
import com.cug.campus_sh_market_server.entity.po.Goods;
import com.cug.campus_sh_market_server.entity.po.User;
import com.cug.campus_sh_market_server.entity.vo.ResultData;
import com.cug.campus_sh_market_server.entity.vo.goods.GoodsAdminVO;
import com.cug.campus_sh_market_server.entity.vo.goods.GoodsVO;
import com.cug.campus_sh_market_server.mapper.vomapper.GoodsAdminVOMapper;
import com.cug.campus_sh_market_server.service.GoodsService;
import com.cug.campus_sh_market_server.util.PageUtil;
import com.cug.campus_sh_market_server.util.QueryWrapperUtil;
import com.cug.campus_sh_market_server.util.UserContext;
import com.nycreativity.elecreativity.entity.vo.PageDataVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author sxt
 * @since 2022-03-22
 */
@Service
public class GoodsServiceImpl
        extends CustomRestfullServiceImpl<Goods, GoodsQueryParam, GoodsAddParam, GoodsPutParam, GoodsVO>
        implements GoodsService {

    @Resource
    private GoodsAdminVOMapper goodsAdminVOMapper;

    @Override
    public PageDataVO<GoodsVO> queryDataByPage(GoodsQueryParam goodsQueryParam, PageParam pageParam, OrderParam orderParam) {
        QueryWrapper<GoodsVO> queryWrapper = getQueryWrapper(goodsQueryParam, orderParam);
        if (queryWrapper != null) {
            queryWrapper.lambda().eq(GoodsVO::getStatus, GoodsStatusEnum.ON_SALE.getTypeCode());
        }
        Page<GoodsVO> selectPage = voBaseMapper.selectPage(new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()), queryWrapper);
        return PageUtil.convert(selectPage);
    }

    @Override
    public PageDataVO<GoodsAdminVO> queryAdminGoodsByPage(GoodsQueryParam goodsQueryParam, PageParam pageParam, OrderParam orderParam) {
        QueryWrapper<GoodsAdminVO> queryWrapper = QueryWrapperUtil.getQueryWrapper(GoodsAdminVO.class, queryParamClass, goodsQueryParam);
        if (orderParam != null) {
            orderParam.orderConditions().forEach(condition -> {
                if (condition.getFirst()) {
                    queryWrapper.orderByAsc(condition.getSecond());
                } else {
                    queryWrapper.orderByDesc(condition.getSecond());
                }
            });
        }
        if (!checkAdminUser()) {
            queryWrapper.lambda().eq(GoodsAdminVO::getCreator, UserContext.get().getId());
        }
        Page<GoodsAdminVO> selectPage = goodsAdminVOMapper.selectPage(new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()), queryWrapper);
        return PageUtil.convert(selectPage);
    }

    @Override
    public boolean changeGoodsStatus(GoodsStatusUpdateParam goodsStatusUpdateParam) {
        if (goodsStatusUpdateParam == null) {
            return false;
        }
        Integer status = goodsStatusUpdateParam.getStatus();
        if (status == null ||
                (!status.equals(GoodsStatusEnum.ON_SALE.getTypeCode()) &&
                        (!status.equals(GoodsStatusEnum.OFF_SALE.getTypeCode())))) {
            return false;
        }
        Goods goods = getById(goodsStatusUpdateParam.getId());
        if (goods != null) {
            goods.setStatus(status);
            goods.modify();
            return updateById(goods);
        }
        return false;
    }
}
